/*
 * Copyright (C) 2017-2019 Alibaba Group Holding Limited
 */


/******************************************************************************
 * @file     main.c
 * @brief    hello world
 * @version  V1.0
 * @date     17. Jan 2018
 ******************************************************************************/

#include <stdio.h>
#include <stdlib.h>
//#include <stdint.h>
#include "printf.h"
#include "testcase.h"
#include "hal_api.h"
#include "autoconf.h" 
#include "drv.h"
#include "arcdetect.h"
#include "dw_uart_hal.h"  

#define TIMER_BASE_ADDR			(0x10111000UL)
#define TIMER_REG_SIZE			0x14
#define TIMER_LOAD_COUNT(idx)		(TIMER_BASE_ADDR+TIMER_REG_SIZE*idx+0)
#define TIMER_CURRENT_VALLUE(idx)	(TIMER_BASE_ADDR+TIMER_REG_SIZE*idx+0x04)
#define TIMER_CTRL_REG(idx)		(TIMER_BASE_ADDR+TIMER_REG_SIZE*idx+0x08)
#define TIMER_EOI_REG(idx)		(TIMER_BASE_ADDR+TIMER_REG_SIZE*idx+0x0c)
#define TIMER_INT_STATUS(idx)		(TIMER_BASE_ADDR+TIMER_REG_SIZE*idx+0x10)
#define TIMER_INT_ALLSTATUS		(TIMER_BASE_ADDR+0xA0)
#define TIMER_LOAD_COUNT2(idx)		(TIMER_BASE_ADDR+TIMER_REG_SIZE*idx+0xB0)

#define WDT0_BASE_ADDR			(0x10112000UL)
#define WDT0_CR_ADDR			(WDT0_BASE_ADDR)
#define WDT0_TORR_ADDR			(WDT0_BASE_ADDR+0x04)
#define WDT0_CCVR_ADDR			(WDT0_BASE_ADDR+0x08)
#define WDT0_CRR_ADDR			(WDT0_BASE_ADDR+0x0C)

#define MONITOR_BASE_ADDR		(0x10200000UL)
#define	CONFIG_ARC_PARA_ADDR		(MONITOR_BASE_ADDR+0x800UL)
#define	START_ARC_ALL_DONE_ADDR		(MONITOR_BASE_ADDR+0x860UL)
#define	RESULT_ARC_LOCK_ADDR		(MONITOR_BASE_ADDR+0x89CUL)
#define	CONFIG_ARC_READ_ADDR		(MONITOR_BASE_ADDR+0x864UL)

#define METER_BASE_ADDR			(0x10202000UL)
#define CFG_GLB0_ADDR			(METER_BASE_ADDR + 0x00UL)
#define CFG_GLB1_ADDR			(METER_BASE_ADDR + 0x04UL)
#define FIL_RMS_ADDR			(METER_BASE_ADDR + 0x2F0UL)
#define UA_RMS_ADDR			(METER_BASE_ADDR + 0x2F4UL)
#define IA_RMS_ADDR			(METER_BASE_ADDR + 0x300UL)
#define LONG_AVR_NUM			(METER_BASE_ADDR + 0x43CUL)
#define UCHN_ZEROCHK_TH			(METER_BASE_ADDR + 0xC00UL)
#define ICHN_ZEROCHK_TH			(METER_BASE_ADDR + 0xC04UL)
#define IN_ZEROCHK_TH			(METER_BASE_ADDR + 0xC08UL)
#define IL_ZEROCHK_TH			(METER_BASE_ADDR + 0xC0CUL)
#define CHN_ZEROCHK_EN			(METER_BASE_ADDR + 0xC10UL)
#define ZCU_FREQ_EN			(METER_BASE_ADDR + 0xC2CUL)
#define ZCU_FREQ_ADDR			(METER_BASE_ADDR + 0xC30UL)
#define CHN_PHASECHK_EN			(METER_BASE_ADDR + 0xC34UL)
#define CHN_PHASEAMP_EN			(METER_BASE_ADDR + 0xC38UL)
#define UB_PHASE_ADDR			(METER_BASE_ADDR + 0xC3CUL)
#define UC_PHASE_ADDR			(METER_BASE_ADDR + 0xC40UL)
#define IA_PHASE_ADDR			(METER_BASE_ADDR + 0xC44UL)
#define IB_PHASE_ADDR			(METER_BASE_ADDR + 0xC48UL)
#define IC_PHASE_ADDR			(METER_BASE_ADDR + 0xC4CUL)
#define IN_PHASE_ADDR			(METER_BASE_ADDR + 0xC50UL)
#define IL_PHASE_ADDR			(METER_BASE_ADDR + 0xC54UL)
#define IZ_PHASE_ADDR			(METER_BASE_ADDR + 0xC58UL)
#define	ICHN_OCTH_ADDR			(METER_BASE_ADDR + 0xC5CUL)
#define CHN_PEAKCHK_EN			(METER_BASE_ADDR + 0xC70UL)
#define HALF_CYCLE_PTS			(METER_BASE_ADDR + 0xC74UL)

#define SDADC_BASE_ADDR			(0x10203C00UL)
#define CFG_SELF_MEASURE_DATA_ADDR	(SDADC_BASE_ADDR+0x40UL)
#define CFG_SELF_MEASURE_PARA_ADDR	(SDADC_BASE_ADDR+0x3CUL)
#define START_SDADC_ADDR		(SDADC_BASE_ADDR+0x40UL)
#define SDADC_CHANNEL_DATA_ADDR		(SDADC_BASE_ADDR+0x28UL)

#define SARADC_BASE_ADDR		(0x10203800UL)
#define START_SARADC_ADDR		(SARADC_BASE_ADDR+0xB0UL)
#define SARADC_CHANNEL_DATA_ADDR	(SARADC_BASE_ADDR+0x98UL)

#define UART0_DATA_ADDR			(0x10120000UL)
#define UART0_IER_ADDR			(0x10120004UL)
#define UART0_IIR_ADDR			(0x10120008UL)
#define UART0_LCR_ADDR			(0x1012000CUL)
#define UART0_MCR_ADDR			(0x10120010UL)
#define UART0_LSR_ADDR			(0x10120014UL)
#define UART0_BAUD_ADDR			(0x101200C0UL)

typedef struct
{
	uint32_t IaIb_peak;
	uint32_t Ic_peak;
	uint32_t UaUb_peak;
	uint32_t Uc_peak;
}PeakData_t;

uint32_t sys_freq = 0, Taskcnt = 0, timer0cnt = 0, timer1cnt = 0;
PeakData_t g_peak, *p_peak;

static void period_timer0_irq_callback(void *arg)
{
	uint32_t timer_id = *((uint32_t *)arg);
	
	timer0cnt++;
	if(timer0cnt%2000 == 0)
	{
		//printf("SARADC_SMPRATE = %d, %d, %d, %d\n",
		//pSARADC_data->SAMP_RATE_CTRL[0], pSARADC_data->SAMP_RATE_CTRL[1], pSARADC_data->SAMP_RATE_CTRL[2], pSARADC_data->SAMP_RATE_CTRL[3]);
		//printf("CALI_CTRL0 = %08x, CALI_CTRL1 = %08x\n",pSARADC_data->CALI_CTRL[0], pSARADC_data->CALI_CTRL[1]);
	}
}
static void period_timer1_irq_callback(void *arg)
{
	uint32_t timer_id = *((uint32_t *)arg);
	
	timer1cnt++;
	
}

int main(void)
{
    uint32_t i;

    for(i=0;i<10;i++)
    {
        //printf("Hello World!\n");
    }   //printf("Hello_World runs successfully!\n");
    //SystemInit();
    //Drv_Init();
    writel(0xFF,SDADC_BASE_ADDR);
    writel(0x1,START_SDADC_ADDR);
    writel(0x17F000,CFG_SELF_MEASURE_PARA_ADDR);
    
    	writel(0x14, CFG_GLB1_ADDR);	//采样率6.4kHz，bit2=1剩余电流检测使能
    	writel(0x3f, CHN_PEAKCHK_EN);	//峰值检测使能
	writel(64, HALF_CYCLE_PTS);	//半波采样点数
	writel(0x1, ZCU_FREQ_EN);	//频率检测使能
	writel(0x1, CFG_GLB0_ADDR);	
    	Hal_Timer_StartPeriod(DW_TIMER_0_ID, 10, period_timer0_irq_callback);
	irq_set_priority(TIMER0_IRQn,5);
	Hal_Timer_StartPeriod(DW_TIMER_1_ID, 100, period_timer1_irq_callback);
	irq_set_priority(TIMER1_IRQn,4);
	p_peak = &g_peak;
    while(1)
    {
        i++;
        Taskcnt++;
        //for(Taskcnt=0; Taskcnt<1000000; Taskcnt++);
        sys_freq = readl(ZCU_FREQ_ADDR);
        Hal_Cnt_DelayMs(100);
        //p_peak = (PeakData_t *)(METER_BASE_ADDR + 0xCC0);
        g_peak.IaIb_peak = readl(METER_BASE_ADDR + 0xCC0);
        g_peak.Ic_peak = readl(METER_BASE_ADDR + 0xCC4);
        g_peak.UaUb_peak = readl(METER_BASE_ADDR + 0xCC8);
        g_peak.Uc_peak = readl(METER_BASE_ADDR + 0xCCC);
    }

    return 0;
}
